package cn.example.vip.mall.goods.controller;

import cn.example.vip.mall.cart.entity.Cart;
import cn.example.vip.mall.goods.entity.Sku;
import cn.example.vip.mall.goods.service.SkuService;
import cn.example.vip.mall.util.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/sku")
public class SkuController {
	@Autowired
	private SkuService skuService;
	
	/**
	 * 根据Id查询商品信息
	 * @param id
	 */
	@GetMapping("/{id}")
	public RespResult<Sku> querySkuById(@PathVariable("id") String id) {
		Sku sku = skuService.getById(id);
		return RespResult.ok(sku);
	}
	
	/**
	 * 根据type查询AdItems
	 * @param type
	 * @return
	 */
	@GetMapping("/adItems/{type}")
	public List<Sku> typeItems(@PathVariable("type") Integer type) {
		List<Sku> skus = skuService.typeSkuItems(type);
		return skus;
	}
	
	/**
	 * 根据type删除AdItems缓存
	 * @param type
	 * @return
	 */
	@DeleteMapping("/adItems/{type}")
	public RespResult delTypeItems(@RequestParam("type") Integer type) {
		skuService.delTypeSkuItems(type);    // 清理缓存
		return RespResult.ok();
	}
	
	/**
	 * 根据type查询AdItems并更新缓存
	 * @param type
	 * @return
	 */
	@PutMapping("/adItems/{type}")
	public RespResult updateTypeItems(@PathVariable("type") Integer type) {
		skuService.updateTypeSkuItems(type);
		return RespResult.ok();
	}
	
	/**
	 * 库存递减
	 * @param carts
	 * @return
	 */
	@PostMapping("/dcount")
	public RespResult dcount(@RequestBody List<Cart> carts) {
		skuService.dCount(carts);
		return RespResult.ok();
	}

}
